﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevTools.Properties;
using DevTools.Romanizer.Components;
using CharConverter=DevTools.Romanizer.Components.CharConverter;
using StringConverter=DevTools.Romanizer.Components.StringConverter;

namespace DevTools.Romanizer.Forms
{
    public partial class RomanizerForm : XtraForm
    {
        public RomanizerForm()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            uscRecentList.LoadData();

            CharConverter.Instance.MissingItemFound += CharConverter_MissingCharFound;
            CharConverter.Instance.DuplicatedKeyFound += CharConverter_DuplicatedKeyFound;
            CharConverter.Instance.DuplicatedItemsFound += CharConverter_DuplicatedItemsFound;

            CharConverter.Instance.Initialize();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            UserData.Instance.SaveData();
            uscRecentList.SaveData();

            base.OnClosing(e);
        }

        private static void CharConverter_DuplicatedItemsFound(object sender, CharConverter.DuplicatedItemsFoundEventArgs e)
        {
            DuplicatedItemListForm form = new DuplicatedItemListForm(e.Items);
            form.ShowDialog();

            e.UseSystemData = form.UseSystemData;
        }

        private static void CharConverter_DuplicatedKeyFound(object sender, CharConverter.DuplicatedKeyFoundEventArgs e)
        {
            string text = string.Format("사용자 정의 파일에 중복된 항목 \"{0}\"이 있습니다. 프로그램을 종료합니다.", e.Key);
            XtraMessageBox.Show(text);

            Application.Exit();
        }

        private static void CharConverter_MissingCharFound(object sender, CharConverter.MissingItemFoundEventArgs e)
        {
            AddUserItemForm form = new AddUserItemForm(e.Key);
            if (form.ShowDialog() == DialogResult.OK)
            {
                e.Value = form.Value;

                UserData.Instance.Add(e.Key, e.Value);
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            var sources = mmeSource.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
            var targets = new string[sources.Length];

            for (int i = 0; i < sources.Length; i++)
                targets[i] = StringConverter.Instance.Convert(sources[i], uscCasingSelector.SelectedCasing, uscPaddingSelector.SelectedPadding);

            mmeTarget.Text = string.Join(Environment.NewLine, targets);

            if (uscOptionControl.Addable)
                uscRecentList.AddItem(string.Format("{0}\t{1}", mmeSource.Text, mmeTarget.Text));

            if (uscOptionControl.Copyable)
                Clipboard.SetText(mmeTarget.Text);
        }

        private void nbiHelp_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            Process.Start(@"http://kimgwajang.tistory.com/category/Dev%20Tools/한글%20로마자%20변환기");
        }

        private void nbiAbout_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
        }

        private void nbiVersion_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            NavBarItem item = (NavBarItem) sender;
            string url = (string) item.Tag;

            Process.Start(url);
        }
    }
}